//
//  FriendListController.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/5.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "FriendListController.h"
#import "FriendListView.h"
#import "FriendDataCenter.h"
#import "SearchFriendView.h"
#import "ModifyRemarksView.h"
#import "AddFriendView.h"
#import "FriendListMoreView.h"

@interface FriendListController ()<FriendListViewDelegate, SearchFriendViewDelegate, AddFriendViewDelegate ,FriendListMoreViewDelegate, ModifyRemarksViewDelegate>

@property (nonatomic, strong) FriendListView *listView;

@property (nonatomic, strong) FriendDataCenter *dataCenter;

@end

@implementation FriendListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.baseTitle = @"好友列表";
    self.rightBtnTitle = @"返回";
    
    _dataCenter  =[FriendDataCenter new];
    
    
    [self makeFriendListViewUI];
    
    [self loadFriendListData];
}

- (void)makeFriendListViewUI
{
    _listView = [FriendListView new];
    _listView.delegate = self;
    [self.baseView addSubview:_listView];
    [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
    
    [_listView loadFriendListData];
}

- (void)loadFriendListData
{
    [_dataCenter fetchFriendListComplation:^(id responseObject, NSString *error) {
        if (error == nil) {
            _listView.listData = (NSArray *)responseObject;
        }else{
            [YJProgressHud showErrorHud:error];
        }
    }];
}

- (void)headerWithRefreshingData
{
    [self loadFriendListData];
}

- (void)clickSearchFriend:(UIButton *)button
{
    [[SearchFriendView defaultSearch] showSearchView:self];
}

- (void)clickTrueWithAccount:(NSString *)account
{
    if ([NSString checkIsPhone:account] == NO) {
        [YJProgressHud showErrorHud:@"请输入正确的手机号"];
    }else{
        [YJProgressHud showLoadingHud:@"正在查找..."];
        [_dataCenter searchFriendWithPhone:account complation:^(id data, NSString *error) {
            if (error == nil) {
                UserModel *friend = (UserModel *)data;
                [YJProgressHud dissmiss];
                [[SearchFriendView defaultSearch] hideSearchView];
                [[AddFriendView defaultAddView] showAddViewWithFriend:friend delegate:self];
                
            }else{
                [YJProgressHud showErrorHud:error];
            }
        }];
    }
}

- (void)clickAddFriend:(UserModel *)friendInfo
{
    NSString *friendId = [NSString stringWithFormat:@"%lu",friendInfo.userId];
    [_dataCenter addFriendWithFriend:friendId complation:^(NSString *error) {
        if (error == nil) {
            [[AddFriendView defaultAddView] hideAddView];
            [YJProgressHud showSuccessHud:@"邀请信息发送成功"];
        }else{
            [YJProgressHud showErrorHud:error];
        }
    }];
}

- (void)didSelectMore:(CGRect)rect friendModel:(FriendModel *)friendModel
{
    [[FriendListMoreView sharedMore] showMoreWithRect:rect friend:friendModel delegate:self];
}

- (void)didSelectRemarkWithFriend:(FriendModel *)friendModel
{
    [[ModifyRemarksView defaultRemarks] showRemarkWithFriend:friendModel delegate:self];
}

- (void)clickTrueWithFriend:(FriendModel *)friendModel remarks:(NSString *)remarks
{
    if ([NSString isEmptyString:remarks] == YES) {
        [YJProgressHud showErrorHud:@"备注不能为空"];
    }else{
        [YJProgressHud showLoadingHud:@"修改备注中..."];
        [_dataCenter remarkFriend:friendModel remark:remarks complation:^(NSString *error) {
            if (error == nil) {
                [YJProgressHud dissmiss];
                [[ModifyRemarksView defaultRemarks] hideRemarkView];
                [_listView loadFriendListData];
            }else{
                [YJProgressHud showErrorHud:error];
            }
        }];
    }
}

- (void)didSelectDelegateWithFriend:(FriendModel *)friendModel
{
    [self trueDeleteFriend:friendModel];
}

- (void)trueDeleteFriend:(FriendModel *)friendModel
{
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您真的要删除 %@ 好友么？",friendModel.remarks] preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        [YJProgressHud showLoadingHud:@"正在删除..."];
        [_dataCenter deleteFriend:friendModel complation:^(NSString *error) {
            if (error == nil) {
                [YJProgressHud dissmiss];
                [_listView loadFriendListData];
            }else{
                [YJProgressHud showErrorHud:error];
            }
        }];
        
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
